package com.demo.core.redis;

import com.alibaba.fastjson.JSON;
import com.demo.core.constant.CacheTimes;
import com.demo.core.enums.RedisCacheEnum;
import com.demo.core.listener.event.DeleteRedisEvent;
import com.demo.core.utils.StringUtils;
import com.demo.core.utils.ThreadPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author Tan-Bowen
 * @description TODO redis缓存客户端
 * @date 2021/7/27 16:23
 */
@Component
public class RedisCache {

    @Autowired
    ApplicationEventPublisher publisher;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    ThreadPool threadPool;
    private String key;
    private Class<Object> cls;

    public StringRedisTemplate getJedis() {
        return stringRedisTemplate;
    }

    public void del(String key) {
        publisher.publishEvent(new DeleteRedisEvent(this, key));
    }

    public void secondIn(String key, String val, int second) {
        threadPool.execute(() -> {
            this.stringRedisTemplate.opsForValue().set(key, val, second, TimeUnit.SECONDS);
        });

    }

    public void minutesIn(String key, String val, int minutes) {
        threadPool.execute(() -> {
            this.stringRedisTemplate.opsForValue().set(key, val, minutes, TimeUnit.MINUTES);
        });
    }

    public void dayIn(String key, String val, int day) {
        threadPool.execute(() -> {
            this.stringRedisTemplate.opsForValue().set(key, val, day, TimeUnit.DAYS);
        });
    }

    public void forEverIn(String key, String val) {
        threadPool.execute(() -> {
            this.stringRedisTemplate.opsForValue().set(key, val);
        });
    }

    public void cacheOneHour(String val,String key) {
        threadPool.execute(() -> {
            this.stringRedisTemplate.opsForValue().set(key, val, CacheTimes.DEFAULT_ONE_HOUR,TimeUnit.MINUTES);
        });
    }

    public void cacheOneDay(String val,String key) {
        threadPool.execute(() -> {
            this.stringRedisTemplate.opsForValue().set(key, val, CacheTimes.DEFAULT_ONE_DAY,TimeUnit.DAYS);
        });
    }

    public void cacheOneWeek(String val,String key) {
        threadPool.execute(() -> {
            this.stringRedisTemplate.opsForValue().set(key, val, CacheTimes.DEFAULT_ONE_WEEK,TimeUnit.DAYS);
        });
    }

    public void cacheOneMon(String val,String key) {
        threadPool.execute(() -> {
            this.stringRedisTemplate.opsForValue().set(key, val, CacheTimes.DEFAULT_ONE_MOUTH,TimeUnit.DAYS);
        });
    }

    /**
     * @author Tan-Bowen
     * @description TODO 取缓存中object数据
     * @date 2021/7/29 14:22
     * @param [key, cls]
     * @paramType [java.lang.String, java.lang.Class<T>]
     * @return T
     */
    public <T> T get(String key, Class<T> cls) {
        String val = this.stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(val)) {
            return JSON.parseObject(val, cls);
        }
        return null;
    }

    /**
     * @author Tan-Bowen
     * @description TODO 取缓存中List数据
     * @date 2021/7/29 14:21
     * @param [key, cls]
     * @paramType [java.lang.String, java.lang.Class<T>]
     * @return java.util.List<T>
     */
     public <T> List<T> getList(String key, Class<T> cls) {
         String val = this.stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(val)) {
            return  JSON.parseArray(val,cls);
        }
        return new ArrayList<T>();
    }
}
